<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Traits\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class RegisterController extends Controller
{

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }


    protected function validator(array $data)
    {
        if ( !app('Smscode')->verify(
            $data['phone'],
            $data['smscode'],
            !app('Smscode')->registerType
        ))  $data['smscode'] = 0;

        return  Validator::make($data, [
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'phone' => [ 'regex:/^1[345789][0-9]{9}$/', 'unique:user'],
            'smscode' => ['required', 'numeric']
        ], [
            'smscode' => '验证码错误',
            'required' => ':attribute 字段必须存在.',
            'unique' => ':attribute 已经注册 ',
            'min' => ':attribute 至少包含 :min 字符',
            'max' => ':attribute 最多包含 :max 字符',
            'numeric' => ':attribute 必须为数字',
            'confirmed' => ':attribute 不匹配'
            ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     *
     */
    protected function create(array $data)
    {
        return User::create([
            'phone' => $data['phone'],
            'birthday' => strtotime('2000-01-01 00:00:00'),
            'nickname' => $data['phone'],
            'name' => date('Ymdhis'),
            'password' => Hash::make($data['password']),
        ]);
    }
}
